{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Tic Tac Toe - Advanced Solution\n",
    "\n",
    "This solution follows the same basic format as the Complete Walkthrough Solution, but takes advantage of some of the more advanced statements we have learned. Feel free to download the notebook to understand how it works!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Specifically for the iPython Notebook environment for clearing output\n",
    "from IPython.display import clear_output\n",
    "import random\n",
    "\n",
    "# Global variables\n",
    "theBoard = [' '] * 10   # a list of empty spaces\n",
    "available = [str(num) for num in range(0,10)] # a List Comprehension\n",
    "players = [0,'X','O']   # note that players[1] == 'X' and players[-1] == 'O'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Available   TIC-TAC-TOE\n",
      "  moves\n",
      "\n",
      "  7|8|9         | | \n",
      "  -----        -----\n",
      "  4|5|6         | | \n",
      "  -----        -----\n",
      "  1|2|3         | | \n",
      "\n"
     ]
    }
   ],
   "source": [
    "def display_board(a,b):\n",
    "    print('Available   TIC-TAC-TOE\\n'+\n",
    "           '  moves\\n\\n  '+\n",
    "          a[7]+'|'+a[8]+'|'+a[9]+'        '+b[7]+'|'+b[8]+'|'+b[9]+'\\n  '+\n",
    "          '-----        -----\\n  '+\n",
    "          a[4]+'|'+a[5]+'|'+a[6]+'        '+b[4]+'|'+b[5]+'|'+b[6]+'\\n  '+\n",
    "          '-----        -----\\n  '+\n",
    "          a[1]+'|'+a[2]+'|'+a[3]+'        '+b[1]+'|'+b[2]+'|'+b[3]+'\\n')\n",
    "display_board(available,theBoard)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Available   TIC-TAC-TOE\n",
      "  moves\n",
      "\n",
      "  7|8|9         | | \n",
      "  -----        -----\n",
      "  4|5|6         | | \n",
      "  -----        -----\n",
      "  1|2|3         | | \n",
      "\n"
     ]
    }
   ],
   "source": [
    "def display_board(a,b):\n",
    "    print(f'Available   TIC-TAC-TOE\\n  moves\\n\\n  {a[7]}|{a[8]}|{a[9]}        {b[7]}|{b[8]}|{b[9]}\\n  -----        -----\\n  {a[4]}|{a[5]}|{a[6]}        {b[4]}|{b[5]}|{b[6]}\\n  -----        -----\\n  {a[1]}|{a[2]}|{a[3]}        {b[1]}|{b[2]}|{b[3]}\\n')\n",
    "display_board(available,theBoard)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def place_marker(avail,board,marker,position):\n",
    "    board[position] = marker\n",
    "    avail[position] = ' '"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def win_check(board,mark):\n",
    "\n",
    "    return ((board[7] ==  board[8] ==  board[9] == mark) or # across the top\n",
    "    (board[4] ==  board[5] ==  board[6] == mark) or # across the middle\n",
    "    (board[1] ==  board[2] ==  board[3] == mark) or # across the bottom\n",
    "    (board[7] ==  board[4] ==  board[1] == mark) or # down the middle\n",
    "    (board[8] ==  board[5] ==  board[2] == mark) or # down the middle\n",
    "    (board[9] ==  board[6] ==  board[3] == mark) or # down the right side\n",
    "    (board[7] ==  board[5] ==  board[3] == mark) or # diagonal\n",
    "    (board[9] ==  board[5] ==  board[1] == mark)) # diagonal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def random_player():\n",
    "    return random.choice((-1, 1))\n",
    "    \n",
    "def space_check(board,position):\n",
    "    return board[position] == ' '\n",
    "\n",
    "def full_board_check(board):\n",
    "    return ' ' not in board[1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def player_choice(board,player):\n",
    "    position = 0\n",
    "    \n",
    "    while position not in [1,2,3,4,5,6,7,8,9] or not space_check(board, position):\n",
    "        try:\n",
    "            position = int(input('Player %s, choose your next position: (1-9) '%(player)))\n",
    "        except:\n",
    "            print(\"I'm sorry, please try again.\")\n",
    "        \n",
    "    return position"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def replay():\n",
    "    \n",
    "    return input('Do you want to play again? Enter Yes or No: ').lower().startswith('y')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Welcome to Tic Tac Toe!\n",
      "For this round, Player X will go first!\n"
     ]
    }
   ],
   "source": [
    "while True:\n",
    "    clear_output()\n",
    "    print('Welcome to Tic Tac Toe!')\n",
    "    \n",
    "    toggle = random_player()\n",
    "    player = players[toggle]\n",
    "    print('For this round, Player %s will go first!' %(player))\n",
    "    \n",
    "    game_on = True\n",
    "    input('Hit Enter to continue')\n",
    "    while game_on:\n",
    "        display_board(available,theBoard)\n",
    "        position = player_choice(theBoard,player)\n",
    "        place_marker(available,theBoard,player,position)\n",
    "\n",
    "        if win_check(theBoard, player):\n",
    "            display_board(available,theBoard)\n",
    "            print('Congratulations! Player '+player+' wins!')\n",
    "            game_on = False\n",
    "        else:\n",
    "            if full_board_check(theBoard):\n",
    "                display_board(available,theBoard)\n",
    "                print('The game is a draw!')\n",
    "                break\n",
    "            else:\n",
    "                toggle *= -1\n",
    "                player = players[toggle]\n",
    "                clear_output()\n",
    "\n",
    "    # reset the board and available moves list\n",
    "    theBoard = [' '] * 10\n",
    "    available = [str(num) for num in range(0,10)]\n",
    "    \n",
    "    if not replay():\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
